<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="MAC地址段编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                readonly
                disabled
                v-decorator="[ 'macAddressCode', validatorRules.macAddressCode]"
                placeholder="请输入MAC地址段编码"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="MAC地址段名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'macAddressName', validatorRules.macAddressName]"
                placeholder="请输入MAC地址段名称"
              ></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="MAC地址段描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'remark', validatorRules.remark]" placeholder="请输入MAC地址段描述"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="MAC资源池类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                type="list"
                v-decorator="['macAddressResourceType', validatorRules.macAddressResourceType]"
                :trigger-change="true"
                dictCode="mac_resource_pool"
                placeholder="请选择MAC资源池类型"
              />
            </p-form-item>
            <p-form-item label="申请起始码段" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'applyStartCode', validatorRules.applyStartCode]"
                placeholder="请输入申请起始码段"
              ></p-input>
            </p-form-item>
            <p-form-item label="申请终止码段" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input
                v-decorator="[ 'applyStopCode', validatorRules.applyStopCode]"
                placeholder="请输入申请终止码段"
              ></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, putAction, api } from "@/api/manage";
import fSwitch from "@/components/switch";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import { validateDuplicateValue } from "@/utils/util";
import JDictSelectTag from "@/components/dict/JDictSelectTag";

export default {
  name: "MacAddressInfoModal",
  mixins: [SubmitOnchangMixin],
  components: {
    JDictSelectTag,
    fSwitch,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        macAddressCode: {
          rules: [{ required: true, message: "请输入MAC地址段编码!" }],
        },
        macAddressName: {
          rules: [{ required: true, message: "请输入MAC地址段名称!" }],
        },
        remark: {
          rules: [{ required: true, message: "请输入MAC地址段描述!" }],
        },
        macAddressResourceType: {
          rules: [{ required: true, message: "请输入MAC资源池类型!" }],
        },
        applyStartCode: {
          rules: [{ required: true, message: "请输入申请起始码段!" }],
        },
        applyStopCode: {
          rules: [{ required: true, message: "请输入申请终止码段!" }],
        },
      },
      url: {
        add: "/mes-code/mesBcMacAddress",
        edit: "/mes-code/mesBcMacAddress",
      },
    };
  },
  created() {},
  methods: {
    add() {
      this.edit({});
      this.getRuleCodeValue();
    },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [
          { ruleCode: "mes_mac_address_code", columnName: "mes_mac_address_code" },
        ],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          // console.log("生成的编码：" + res.data);
          console.log(223);
          

          console.log(res.data.mes_mac_address_code);
          
          const item = res.data.mes_mac_address_code;
          // this.form.setFieldsValue(pick(item, Object.keys(item)));

          // this.form.macAddressCode = res.data.mes_mac_address_code

          this.form.setFieldsValue( {macAddressCode: item } )
        }
      });
    },

    edit(record) {
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(
          pick(
            this.model,
            "macAddressCode",
            "macAddressName",
            "remark",
            "applyStartCode",
            "applyStopCode",
            'macAddressResourceType'
          )
        );
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          let formData = Object.assign(this.model, values);
          this.formData = Object.assign(this.model, values);
          //新增触发
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
            if (this.EnhanceJS.afterAdd) {
              //新增之前
              let Obj = eval(this.EnhanceJS.afterAdd);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          } else {
            //编辑触发
            httpurl += this.url.edit;
            method = "put";
            if (this.EnhanceJS.afterEdit) {
              //编辑之前
              let Obj = eval(this.EnhanceJS.afterEdit);
              new Obj(this);
            } else {
              console.log("表单提交数据", formData);
              that.modalFrom(httpurl, formData, method);
            }
          }
        }
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          "macAddressCode",
          "macAddressName",
          "remark",
          "applyStartCode",
          "applyStopCode",
          'macAddressResourceType'
        )
      );
    },
  },
};
</script>